package com.vhans.bapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.EmailDTO;
import com.vhans.db.model.dto.user.UserInfoDTO;
import com.vhans.db.model.dto.user.UserPasswdDTO;
import com.vhans.db.model.vo.agreement.MyAgreement;
import com.vhans.db.model.vo.comment.CommentVO;
import com.vhans.db.model.vo.quiz.QuizInfoVO;
import com.vhans.db.model.vo.quiz.QuizVO;
import com.vhans.db.model.vo.record.RecordInfoVO;
import com.vhans.db.model.vo.record.RecordVO;
import com.vhans.db.model.vo.user.UserInfoVO;
import com.vhans.db.service.IBUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户控制器
 *
 * @author vhans
 **/
@Api(tags = "用户模块")
@RestController
public class UserController extends BaseController {

    @Autowired
    private IBUserService userService;

    /**
     * 获取用户登录信息
     *
     * @return {@link UserInfoVO} 用户信息
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户登录信息")
    @GetMapping("/user/getUserInfo")
    public Result<UserInfoVO> getUserInfo() {
        return Result.success(userService.getUserInfo());
    }

    /**
     * 获取用户发布的约起
     *
     * @return 发布的约起
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户发布的约起")
    @GetMapping("/user/getAgree/issue")
    public Result<List<MyAgreement>> getIssueAgreement() {
        return Result.success(userService.getIssueAgreement());
    }

    /**
     * 获取用户参与的约起
     *
     * @return 参与的约起
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户参与的约起")
    @GetMapping("/user/getAgree/take")
    public Result<List<MyAgreement>> getTakeAgreement() {
        return Result.success(userService.getTakeAgreement());
    }

    /**
     * 获取用户发表的记录
     *
     * @return 发表的记录
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户发表的记录")
    @GetMapping("/user/getRecord/issue")
    public TableDataInfo getIssueRecord() {
        startPage();
        List<RecordVO> list = userService.getIssueRecord();
        return getDataTable(list);
    }

    /**
     * 获取用户发表的题目
     *
     * @return 发表的题目
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户发表的题目")
    @GetMapping("/user/getQuiz/issue")
    public TableDataInfo getIssueQuiz() {
        startPage();
        List<QuizVO> list = userService.getIssueQuiz();
        return getDataTable(list);
    }

    /**
     * 获取用户收藏的记录
     *
     * @return 收藏的记录
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户收藏的记录")
    @GetMapping("/user/getRecord/collect")
    public Result<List<RecordInfoVO>> getCollectRecord() {
        return Result.success(userService.getCollectRecord());
    }

    /**
     * 获取登录用户收藏的题目
     *
     * @return 收藏的题目
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户收藏的题目")
    @GetMapping("/user/getQuiz/collect")
    public Result<List<QuizInfoVO>> getCollectQuiz() {
        return Result.success(userService.getCollectQuiz());
    }

    /**
     * 获取用户点赞的记录
     *
     * @return 点赞的记录
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户点赞的记录")
    @GetMapping("/user/getRecord/like")
    public Result<List<RecordInfoVO>> getLikeRecord() {
        return Result.success(userService.getLikeRecord());
    }

    /**
     * 获取用户点赞的题目
     *
     * @return 点赞的题目
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户点赞的题目")
    @GetMapping("/user/getQuiz/like")
    public Result<List<QuizInfoVO>> getLikeQuiz() {
        return Result.success(userService.getLikeQuiz());
    }

    /**
     * 获取用户点赞的评论
     *
     * @return 点赞的评论
     */
    @SaCheckLogin
    @ApiOperation(value = "获取用户点赞的评论")
    @GetMapping("/user/getComment/like")
    public Result<List<CommentVO>> getLikeComment() {
        return Result.success(userService.getLikeComment());
    }

    /**
     * 修改用户邮箱
     *
     * @param email 邮箱信息
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "修改用户邮箱")
    @PutMapping("/user/email")
    public Result<?> updateUserEmail(@Validated @RequestBody EmailDTO email) {
        userService.updateUserEmail(email);
        return Result.success();
    }

    /**
     * 修改用户头像
     *
     * @param file 文件
     * @return {@link Result<String>} 头像地址
     */
    @SaCheckLogin
    @ApiOperation(value = "修改用户头像")
    @PostMapping("/user/avatar")
    public Result<String> updateUserAvatar(@RequestParam(value = "file") MultipartFile file) {
        return Result.success(userService.updateUserAvatar(file));
    }

    /**
     * 修改用户密码
     *
     * @param password 密码
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "修改用户密码")
    @PutMapping("/user/password")
    public Result<?> updateAdminPassword(@Validated @RequestBody UserPasswdDTO password) {
        userService.updatePassword(password);
        return Result.success();
    }

    /**
     * 修改用户信息
     *
     * @param userInfo 用户信息
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "修改用户信息")
    @PutMapping("/user/update")
    public Result<?> updateUserInfo(@Validated @RequestBody UserInfoDTO userInfo) {
        userService.updateUserInfo(userInfo);
        return Result.success();
    }
}